package com.enlorenz.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class SocketUtils {
	
	/**
	 * 获取Ip
	 * @return
	 */
	public static String getIp(){
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String ip=addr.getHostAddress().toString();//获得本机IP
			return ip;
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Channel是否连接
	 * @param channel
	 * @return
	 */
	public static boolean isChannelConnected(SocketChannel channel){
		return channel.isOpen()&&channel.isConnected();
	}
	
	/**
	 * 选择器是否正常开启
	 * @param selector
	 */
	public static boolean isSelectorOpen(Selector selector){
		return null != selector&& selector.isOpen();
	}
	
	/**
	 * 关闭选择器
	 * @param selector
	 */
	public static void closeSelector(Selector selector){
		//关闭选择器
		if (isSelectorOpen(selector)) {
			try {
				selector.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 服务是否正常开启
	 * @param server
	 */
	public static boolean isServerSocketChannelOpen(ServerSocketChannel server){
		return null != server&& server.isOpen();
	}
	
	/**
	 * 关闭Socket服务端
	 * @param server
	 */
	public static void closeServerSocketChannel(ServerSocketChannel server){
		//关闭Socket服务
		if (isServerSocketChannelOpen(server)) {
			try {
				server.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 关闭channel
	 * @param channel
	 */
	public static void closeChannel(SocketChannel channel){
		if(null == channel){
			return;
		}
		try {
			channel.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 关闭socketserver
	 * @param socketserver
	 */
	public static void closeSocketServer(ServerSocket server){
		if(null == server){
			return;
		}
		try {
			server.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭socket
	 * @param socket
	 */
	public static void closeSocket(Socket socket){
		if(null == socket){
			return;
		}
		try {
			socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 是否连接
	 * @param socket
	 * @return
	 */
	public static boolean isConnected(Socket socket){
		if (null == socket){
			return false;
		}
		return socket.isConnected();
	}
	
	/**
	 * 写出然后读取
	 * @param ip
	 * @param port
	 * @param msg
	 * @return
	 */
	public static String writeAndRead(String ip,int port,String msg){
		return writeAndRead(ip, port, msg.getBytes());
	}
	
	public static String writeAndRead(String ip,int port,byte [] buff){
		Socket socket=null;
		OutputStream outputStream=null;
		InputStream inputStream=null;
		try{
			socket=new Socket(ip,port);
			outputStream=socket.getOutputStream();
			
			outputStream.write(buff);
			outputStream.flush();
			byte [] bb=new byte[1024];
			inputStream=socket.getInputStream();
			int len=inputStream.read(bb);
			return new String(bb,0,len,"utf-8");
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeSocket(socket);
			StreamUtils.closeStream(outputStream);
			StreamUtils.closeStream(inputStream);
		}
		return null;
	}
	
}
